package com.ccpdown.view;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import com.ccpdown.view.R;
import com.ccpdown.util.StringUtil;
import com.ccpdown.util.show.BitmapDownloader;
import com.ccpdown.util.socket.NetWorkUtil;
import com.ccpdown.util.xml.CtrXmlUtil;
import com.ccpdown.vo.Software;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class DownloadView extends Activity {

	private ImageView ivDownSoftIcon;
	private TextView tvDownSoftName;
	private TextView tvDownWriter;
	private TextView tvDownSoftSq;
	private ImageView ivDownSoftStart;
	private TextView tvDownSize;
	private TextView tvDownSoftSay;
	private Button btnDown;
	private ImageView ivSoftImg;

	private String path;
	private String currentFilePath;
	private String currentTempFilePath;
	private String strURL;
	private String fileEx;
	private String fileNa;
	private Software software;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_file);
		getView();
		doing();
	}

	private void getView() {
		ivDownSoftIcon = (ImageView) findViewById(R.id.ivDownSoftIcon);
		tvDownSoftName = (TextView) findViewById(R.id.tvDownSoftName);
		tvDownWriter = (TextView) findViewById(R.id.tvDownWriter);
		tvDownSoftSq = (TextView) findViewById(R.id.tvDownSoftSq);
		ivDownSoftStart = (ImageView) findViewById(R.id.ivDownSoftStart);
		tvDownSize = (TextView) findViewById(R.id.tvDownSize);
		tvDownSoftSay = (TextView) findViewById(R.id.tvDownSoftSay);
		btnDown = (Button) findViewById(R.id.btnDown);
		ivSoftImg= (ImageView) findViewById(R.id.ivSoftImg);
	}

	private void doing() {
		//layoutDown.setOnTouchListener(l);
		Bundle bundle = this.getIntent().getExtras();
		String softId = bundle.getString("softId");
		path = StringUtil.ROOTURL+StringUtil.SOFTURL+ softId;
		software = CtrXmlUtil.getSoftware(path);
		BitmapDownloader.setSocketImg(ivDownSoftIcon, software.iconPath, getResources().getDrawable(R.drawable.defaulticon));
		tvDownSoftName.setText(software.getSoftName());
		tvDownWriter.setText(software.getWriter());
		tvDownSoftSq.setText(StringUtil.getSoftSq(software.getSoftSq()));
		tvDownSize.setText(software.getFileSize());
		strURL = software.getPathStr();
		tvDownSoftSay.setText(StringUtil.getSoftSay(software.getSoftSay()));
		btnDown.setOnClickListener(btnDownListen);
		BitmapDownloader.setSocketImg(ivSoftImg, software.softPic, null);
	}

	private void getFile(final String strPath) {
		try {
			if (strPath.equals(currentFilePath)) {
				getDataSource(strPath);
			} else {
				currentFilePath = strPath;
				Runnable r = new Runnable() {

					@Override
					public void run() {
						try {
							getDataSource(strPath);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				};
				new Thread(r).start();
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void getDataSource(String strPath) throws Exception {
		if (!URLUtil.isNetworkUrl(strPath)) {
			throw new RuntimeException("网络连接出现问题");
		}
		URL myURL = new URL(strPath);
		URLConnection conn = myURL.openConnection();
		conn.connect();
		InputStream is = conn.getInputStream();
		if (is != null) {
			File tmpFile = File.createTempFile(fileNa, "." + fileEx);
			currentTempFilePath = tmpFile.getAbsolutePath();
			FileOutputStream fos = new FileOutputStream(tmpFile);
			byte[] buf = new byte[128];
			int numread = 1;
			while (true) {
				numread = is.read(buf);
				if (numread <= 0) {
					break;
				}
				fos.write(buf, 0, numread);
			}
			openFile(tmpFile);
			fos.close();
		} else {
			throw new RuntimeException("文件传输产生错误");
		}
		is.close();
	}

	//打开文件；
	private void openFile(File file) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);

		String type = getType(file);
		intent.setDataAndType(Uri.fromFile(file), type);
		startActivity(intent);
	}

	//取得文件的后缀名；
	private String getType(File file) {
		String type = "";
		String fName = file.getName();
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();
		if (end.equals("apk")) {
			type = "application/vnd.android.package-archive";
		}
		return type;
	}

	private OnClickListener btnDownListen = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {

			fileEx = strURL.substring(strURL.lastIndexOf(".") + 1,
					strURL.length()).toLowerCase();
			fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,
					strURL.lastIndexOf("."));
			getFile(strURL);

		}
	};

}
